<?php

namespace addons\organization\models;

use Yii;

/**
 * @SWG\Definition(
 *   definition="OOrganisationRelationMiddle",
 *   type="object",
 *   allOf={
 *       @SWG\Schema(
 *           required={"tag"},
 *           @SWG\Property(property="ORGANISATION_RELATION_MIDDLE_ID", type="integer",description="中间表ID"),
 *           @SWG\Property(property="FUNCTION_ID", type="integer",description="职能类型ID"),
 *           @SWG\Property(property="ORGANISATION_ID",  type="integer",format="int32",description="组织ID"),
 *           @SWG\Property(property="ORGANISATION_PID",type="integer",format="int32",description="父组织ID"),
 *           @SWG\Property(property="ENTITY_STATE", type="integer",description="是否实体")
 *       )
 *   }
 * )
 */
class OOrganisationRelationMiddle extends \yii\swoole\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'o_organisation_relation_middle';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['FUNCTION_ID', 'ENTITY_STATE', 'ORGANISATION_ID', 'ORGANISATION_PID'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ORGANISATION_RELATION_MIDDLE_ID' => Yii::t('organization', '中间表ID'),
            'FUNCTION_ID' => Yii::t('organization', '职能类型ID'),
            'ENTITY_STATE' => Yii::t('organization', '是否实体'),
            'ORGANISATION_ID' => Yii::t('organization', '组织ID'),
            'ORGANISATION_PID' => Yii::t('organization', '父组织ID'),
        ];
    }

    //组织编码-编码
    public function getO_organisationt()
    {
        return $this->hasOne(OOrganisation::className(), ['ORGANISATION_ID' => 'ORGANISATION_ID'])->where(['ot.ORGANISATION_STATE' => 1])->alias('ot')->joinWith('o_grouping');
    }

    //组织编码-编码
    public function getO_organisationc()
    {
        return $this->hasOne(OOrganisation::className(), ['ORGANISATION_ID' => 'ORGANISATION_ID'])->where(['oc.ORGANISATION_STATE' => 1])->alias('oc')->joinWith(['b_channel', 'user_warehouse']);
    }

    //组织编码-编码
    public function getO_organisation()
    {
        return $this->hasOne(OOrganisation::className(), ['ORGANISATION_ID' => 'ORGANISATION_ID']);
    }

    //组织编码-编码
    public function getAccountingperiod()
    {
        return $this->hasOne(\addons\finance\models\AcAccountingPeriod::className(), ['ORGANISATION_ID' => 'ORGANISATION_ID']);
    }

    public function after_AIndex($body, $class = null)
    {
        $str = Yii::$app->rpc->create('base')->send([['\addons\common\base\modellogic\filterLogic', 'get_api'], []])->recv();
        if ($str) {
            $user = Yii::$app->session->get('organization');
            $newArry = [];
            if ($user) {
                foreach ($user as $index => $item) {
                    foreach ($body as $index1 => $item1) {
                        if ($item == $item1['ORGANISATION_ID']) {
                            array_push($newArry, $item1);
                        }
                    }
                }
            }
            $body = $newArry;
        }

        return parent::after_AIndex($body, $class); // TODO: Change the autogenerated stub
    }

}
